<?php

namespace App\Services;

use App\Models\AdminMenu;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;

class AdminMenuService extends BaseService
{
    /**
     * 创建菜单
     * @param $data
     * @return bool
     */
    public function create($data): bool
    {
        try {
            AdminMenu::query()->create($data);
            return true;
        } catch (\Exception $exception) {
            return false;
        }
    }

    /**
     * 根据ID修改菜单信息
     * @param $data
     * @param $id
     * @return bool
     */
    public function updateById($data,$id): bool
    {
        try {
            $result = AdminMenu::query()->where('id','=',$id)->update($data);
            if ($result) {
                return true;
            }
            return false;
        } catch (\Exception $exception) {
            return false;
        }
    }

    /**
     * 根据ID获取菜单详情
     * @param $id
     * @return array
     */
    public function getById($id): array
    {
        $result = AdminMenu::query()->where('id','=',$id)->first();
        if (is_null($result)) {
            return [];
        }
        return $result->toArray();
    }

    /**
     * 根据ID删除菜单信息
     * @param $id
     * @return bool
     */
    public function deleteById($id): bool
    {
        try {
            $result = AdminMenu::query()->where('id','=',$id)->delete();
            if ($result) {
                return true;
            }
            return false;
        } catch (\Exception $exception) {
            return false;
        }
    }

    /**
     * 分页展示菜单信息
     * @param $parentId
     * @param $pageSize
     * @param $pageNum
     * @return LengthAwarePaginator
     */
    public function list($parentId,$pageSize,$pageNum): LengthAwarePaginator
    {
        return AdminMenu::query()->where('parent_id','=',$parentId)
            ->paginate($pageSize, ['*'],'page',$pageNum);
    }

    /**
     * 获取所有菜单
     * @return array
     */
    public function listAll(): array
    {
        $result = AdminMenu::query()->get();
        if ($result->count() > 0) {
            $result->toArray();
        }
        return [];
    }

}
